﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QUANLIHOCSINH.BussinessObject;
using QUANLIHOCSINH.BussinessLogic;
namespace QUANLIHOCSINH
{
    enum Status_QuanLiMonHoc { View, Insert, Update, Delete };
    public partial class FrmQuanLiMonHoc : Form
    {
        Status_QuanLiMonHoc TrangThai = Status_QuanLiMonHoc.View;
        MONHOCBUS monhoc = new MONHOCBUS();
        public FrmQuanLiMonHoc()
        {
            InitializeComponent();
        }
        public void resetText()
        {
            txtHeSo.Text = "";
            txtSoTiet.Text = "";
            txtTenMon.Text = "";
        }
        private void EnableButton(bool Enable)
        {
            btnThem.Visible = !Enable;
            btnSua.Visible = !Enable;
           
            btnCapNhat.Visible = Enable;
            btnHuy.Visible = Enable;

            gbmonhoc.Enabled = Enable;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            resetText();
            txtMaMon.Text = monhoc.NextID();
            EnableButton(true);
            btnThem.Visible = false;
            TrangThai = Status_QuanLiMonHoc.Insert;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
            if (dgvMonHoc.Rows.Count > 1)
            {
                int r = dgvMonHoc.CurrentRow.Index;
               txtMaMon.Text = dgvMonHoc.Rows[r].Cells["MaMon"].Value.ToString();
               txtTenMon.Text = dgvMonHoc.Rows[r].Cells["TenMon"].Value.ToString();
               txtSoTiet.Text = dgvMonHoc.Rows[r].Cells["SoTiet"].Value.ToString();
               txtHeSo.Text = dgvMonHoc.Rows[r].Cells["HeSo"].Value.ToString();
     
            }
            EnableButton(true);
            btnSua.Visible = false;
            TrangThai = Status_QuanLiMonHoc.Update;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            EnableButton(false);
        }
        private MONHOC LayThongTinMonHoc()
        {
            MONHOC mh = new MONHOC();
            mh.MaMon = txtMaMon.Text;
            mh.TenMon = txtTenMon.Text;
            mh.SoTiet = txtSoTiet.Text;
            mh.HeSo = txtHeSo.Text;
            return mh;
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (TrangThai == Status_QuanLiMonHoc.Insert)
            {
                if (monhoc.ThemMonHoc(LayThongTinMonHoc()) == true)
                {
                    
                    FrmQuanLiMonHoc_Load(sender, e);
                }
            }
            if (TrangThai == Status_QuanLiMonHoc.Update)
            {
                if (monhoc.SuaMonHoc(LayThongTinMonHoc()) == true)
                {
                    FrmQuanLiMonHoc_Load(sender, e);
                }
            }
        }

        private void FrmQuanLiMonHoc_Load(object sender, EventArgs e)
        {
            dgvMonHoc.DataSource = monhoc.LayDanhSachMonHoc();
            EnableButton(false);
        }
    }
}
